package com.raycloud.dao;

import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.raycloud.common.Result;
import com.raycloud.query.EndProjectQuery;
import com.raycloud.pojo.EndProject;

/**
 * @author lvduo (lvduo940613@gmail.com)
 * @date 2017-04-27
 */

@Repository
public class EndProjectDAO {

    @Resource
    SqlMapClientTemplate sqlMapClientTemplate;

    public Integer addEndProject(EndProject endProject) {
        return (Integer) this.sqlMapClientTemplate.insert("EndProject.insertEndProject", endProject);
    }

    /**
     * 根据主键查找
     */
    public EndProject getEndProjectByKey(String projectName) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("projectName", projectName);
        EndProject result = (EndProject) this.sqlMapClientTemplate.queryForObject(
                "EndProject.getEndProjectByKey", params);
        return result;
    }

    /**
     * 根据主键批量查找
     *
     * @
     */
    public List<EndProject> getEndProjectByKeys(List<String> projectNameList) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("keys", projectNameList);
        return (List<EndProject>) this.sqlMapClientTemplate.queryForList("EndProject.getEndProjectsByKeys", params);
    }

    /**
     * 根据主键删除
     *
     * @return
     * @
     */
    public Integer deleteByKey(String projectName) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("projectName", projectName);
        Integer row = (Integer) this.sqlMapClientTemplate.delete("EndProject.deleteByKey", params);
        return row;
    }

    /**
     * 根据主键批量删除
     *
     * @return
     * @
     */
    public Integer deleteByKeys(List<String> projectNameList) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("keys", projectNameList);

        Integer row = (Integer) this.sqlMapClientTemplate.delete("EndProject.deleteByKeys", params);
        return row;
    }

    /**
     * 根据主键更新
     *
     * @return
     * @
     */
    public Integer updateEndProjectByKey(EndProject endProject) {
        return (Integer) this.sqlMapClientTemplate.update("EndProject.updateEndProjectByKey", endProject);
    }

    @SuppressWarnings("unchecked")
    public Result<EndProject> getEndProjectListWithPage(EndProjectQuery endProjectQuery) {
        Result<EndProject> rs = new Result<EndProject>();

        rs.setCount((Integer) this.sqlMapClientTemplate.queryForObject("EndProject.getEndProjectListCount", endProjectQuery));
        if (endProjectQuery.getFields() != null && endProjectQuery.getFields() != "") {
            rs.setList((List<EndProject>) this.sqlMapClientTemplate.queryForList("EndProject.getEndProjectListWithPageFields", endProjectQuery));
        } else {
            rs.setList((List<EndProject>) this.sqlMapClientTemplate.queryForList("EndProject.getEndProjectListWithPage", endProjectQuery));
        }

        return rs;
    }

    @SuppressWarnings("unchecked")
    public List<EndProject> getEndProjectList(EndProjectQuery endProjectQuery) {
        if (endProjectQuery.getFields() != null && endProjectQuery.getFields() != "") {
            return (List<EndProject>) this.sqlMapClientTemplate.queryForList("EndProject.getEndProjectListFields", endProjectQuery);
        }
        return (List<EndProject>) this.sqlMapClientTemplate.queryForList("EndProject.getEndProjectList", endProjectQuery);
    }


}